<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class goods_act_uqcms extends control
{
    function __construct()
    {
        $this->spid = $_SESSION['shop']['spid'];
        parent::__construct();
    }
    public function seckill()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $type = isset($_GET['type']) ? $_GET['type'] : '0';
        $now_time = time();
        $wsql = '';
        if ($type === '0') {
            $wsql .= ' and start_time > ' . $now_time;
        } elseif ($type == '1') {
            $wsql .= ' and start_time <= ' . $now_time . ' and end_time >= ' . $now_time;
        } elseif ($type == '2') {
            $wsql .= ' and end_time < ' . $now_time;
        }
        $wsql = ' where shop_id = ' . $this->spid . ' ' . $wsql . ' ';
        $result = $this->db->get_all("select * from " . table('promote_seckill') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            foreach ($result as $k => $v) {
                $goods_id[] = $v['gid'];
            }
            if ($goods_id) {
                $goods_in = implode(',', $goods_id);
                $goods_result = $this->db->get_all("select id,shop_id,title,images,price,status,stock from " . table('goods') . " where id in (" . $goods_in . ") ");
                foreach ($goods_result as $v) {
                    $goods_arr[$v['id']] = $v;
                }
            }
            foreach ($result as $v) {
                $v['start_time'] = _date($v['start_time']);
                $v['end_time'] = _date($v['end_time']);
                $v['addtime'] = _date($v['addtime']);
                $v['goods'] = $goods_arr[$v['gid']];
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('promote_seckill') . $wsql);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function seckill_edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('promote_seckill') . " where shop_id = " . $this->spid . " and id = " . $id);
            if ($row['id']) {
                $row['start_time'] = _date($row['start_time']);
                $row['end_time'] = _date($row['end_time']);
                $this->assign('row', $row);
                $this->assign('goods', $this->promote_goods($row['gid']));
            }
        } else {
            $this->assign('goods', $this->promote_goods());
        }
        $this->display();
    }
    public function seckill_save()
    {
        $this->db->autocommit();
        $gid = P('gid', '请选择一个产品');
        $data['name'] = P('name', '名称不能为空');
        $price_arr = isset($_POST['seckill_price']) ? $_POST['seckill_price'] : error_json('价格提交错误');
        $data['price'] = current($price_arr);
        $data['gid'] = $gid;
        $data['start_time'] = !empty($_POST['start_time']) ? strtotime($_POST['start_time']) : error_json('开始时间不能为空');
        $data['end_time'] = !empty($_POST['end_time']) ? strtotime($_POST['end_time']) : error_json('结束时间不能为空');
        $data['status'] = P('status', '', '0');
        $id = P('id', 0);
        if ($id) {
            $query = $this->db->update(table('promote_seckill'), $data, 'shop_id = ' . $this->spid . ' and id = ' . $id);
        } else {
            $row = $this->db->get_one("select * from " . table('promote_seckill') . " where shop_id = " . $this->spid . " and gid = " . $gid);
            if ($row['id']) {
                error_json('该产品已经存在优惠中');
            } else {
                $data['shop_id'] = $this->spid;
                $data['addtime'] = time();
                $query = $this->db->add(table('promote_seckill'), $data);
                $id = $this->db->insert_id();
            }
        }
        if ($price_arr) {
            $query2 = $this->db->delete(table('promote_seckill_sku'), 'gid = ' . $gid);
            $add_sql = '';
            foreach ($price_arr as $k => $v) {
                $add_sql .= "('" . $this->spid . "', '" . $id . "', '" . $gid . "', '" . $k . "', '" . $v . "'),";
            }
            $insert_sql = "INSERT INTO `" . table("promote_seckill_sku") . "` (`shop_id`, `sid`, `gid`, `sku_id`, `price`) VALUES ";
            $insert_sql .= rtrim($add_sql, ',') . ";";
            $query3 = $this->db->query($insert_sql);
        } else {
            $this->db->rollback();
            error_json('保存失败');
        }
        if ($query && $query2 && $query3) {
            $this->db->commit();
            $result['error'] = '0';
            echo json_encode($result);
        } else {
            $this->db->rollback();
            error_json('保存失败');
        }
    }
    public function seckill_goods_sku()
    {
        $type = isset($_GET['type']) ? $_GET['type'] : '0';
        $gid = isset($_GET['gid']) ? $_GET['gid'] : error_json('提交错误');
        $grow = $this->db->get_one("select * from " . table('goods') . " where shop_id = " . $this->spid . " and id = " . $gid);
        if ($grow['id']) {
            if ($type == '2') {
                $sk_query = $this->db->get_all("select id,gid,sku_id,price from " . table('promote_group_sku') . " where gid = " . $gid);
            } else {
                $sk_query = $this->db->get_all("select id,gid,sku_id,price from " . table('promote_seckill_sku') . " where gid = " . $gid);
            }
            if ($sk_query) {
                foreach ($sk_query as $v) {
                    $pr_arr[$v['sku_id']] = $v;
                }
            }
            $result = $this->db->get_all("select * from " . table('goods_sku_index') . " where gid = " . $grow['id']);
            if ($result) {
                foreach ($result as $v) {
                    if ($v['name']) {
                        $arr = unserialize($v['name']);
                        if ($arr) {
                            $sku_name = '';
                            foreach ($arr as $vv) {
                                $sku_name .= $vv[0] . '：' . $vv[1] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                            }
                            $v['sku_name'] = $sku_name;
                        }
                        unset($arr);
                    } else {
                        $v['sku_name'] = '/';
                    }
                    if (isset($pr_arr[$v['id']]['price'])) {
                        $v['act_price'] = $pr_arr[$v['id']]['price'];
                    } else {
                        $v['act_price'] = '';
                    }
                    $sku_list[] = $v;
                }
                $data['sku_list'] = $sku_list;
                right_json($data);
            } else {
                error_json('获取数据错误');
            }
        } else {
            error_json('获取数据错误');
        }
    }
    public function promote_goods($gid = null, $ptype = '1')
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '5';
        if ($gid) {
            $list = $this->db->get_all("select * from " . table('goods') . " where shop_id = " . $this->spid . " and id = " . $gid);
            $data['list'] = $list;
            return $data;
        } else {
            if ($ptype == '1') {
                $from_sql = " from " . table('goods') . " as g left join " . table('promote_seckill') . " as p on g.id = p.gid where g.shop_id = " . $this->spid . " and p.id is null ";
            } elseif ($ptype == '2') {
                $from_sql = " from " . table('goods') . " as g left join " . table('promote_group') . " as p on g.id = p.gid where g.shop_id = " . $this->spid . " and p.id is null ";
            }
            $sql = "select g.id,g.cid,g.shop_id,g.title,g.price,g.images,g.stock,p.id as gid,p.shop_id,p.gid,p.price as act_price " . $from_sql . " order by g.id desc limit " . ($page - 1) * $page_size . "," . $page_size;
            $result = $this->db->get_all($sql);
            if ($result) {
                foreach ($result as $v) {
                    $list[] = $v;
                }
                $data['list'] = $list;
                $total = $this->db->get_one("select count(g.id) as total " . $from_sql);
                if ($page_size < $total['total']) {
                    $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
                }
                return $data;
            } else {
                return false;
            }
        }
    }
    public function seckill_del()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('提交错误');
        $row = $this->db->get_one("select * from " . table('promote_seckill') . " where shop_id = " . $this->spid . " and id = " . $id);
        if ($row['id']) {
            $this->db->autocommit();
            $query = $this->db->delete(table('promote_seckill'), 'shop_id = ' . $this->spid . ' and id = ' . $row['id']);
            $query2 = $this->db->delete(table('promote_seckill_sku'), 'sid = ' . $row['id']);
            if ($query && $query2) {
                $this->db->commit();
                showmsg('删除成功');
            } else {
                $this->db->rollback();
                showmsg('删除失败');
            }
        } else {
            error_404();
        }
    }
    function reduce()
    {
        $query = $this->db->get_all("select * from " . table('promote_reduce') . " where shop_id = " . $this->spid);
        if ($query) {
            foreach ($query as $k => $v) {
                $v['start_time'] = _date($v['start_time']);
                $v['end_time'] = _date($v['end_time']);
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $this->assign('data', $list);
        }
        $this->display();
    }
    public function reduce_edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('promote_reduce') . " where shop_id = " . $this->spid . " and id = " . $id);
            if ($row['id']) {
                $row['start_time'] = date('Y-m-d H:i', $row['start_time']);
                $row['end_time'] = date('Y-m-d H:i', $row['end_time']);
                $this->assign('row', $row);
            } else {
                error_404();
            }
        }
        $this->display();
    }
    public function reduce_save()
    {
        $data['name'] = !empty($_POST['name']) ? $_POST['name'] : error_json('名称不能为空');
        $data['start_time'] = !empty($_POST['start_time']) ? strtotime($_POST['start_time']) : error_json('开始时间不能为空');
        $data['end_time'] = !empty($_POST['end_time']) ? strtotime($_POST['end_time']) : error_json('结束时间不能为空');
        $data['total_price'] = !empty($_POST['total_price']) ? $_POST['total_price'] : error_json('总消费金额不能为空');
        $data['reduce_price'] = !empty($_POST['reduce_price']) ? $_POST['reduce_price'] : error_json('减免金额不能为空');
        $data['status'] = !empty($_POST['status']) ? $_POST['status'] : '1';
        $data['addtime'] = time();
        $id = isset($_POST['id']) ? $_POST['id'] : false;
        if ($id) {
            $query = $this->db->update(table('promote_reduce'), $data, 'shop_id = ' . $this->spid . ' and id = ' . $id);
        } else {
            $data['shop_id'] = $this->spid;
            $query = $this->db->add(table('promote_reduce'), $data);
        }
        if ($query) {
            $result['error'] = '0';
            echo json_encode($result);
        } else {
            error_json('保存失败');
        }
    }
    function reduce_del()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('提交错误');
        $query = $this->db->delete(table('promote_reduce'), 'shop_id = ' . $this->spid . ' and id = ' . $id);
        if ($query) {
            showmsg('删除成功');
        } else {
            showmsg('删除失败');
        }
    }
    function group()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $type = isset($_GET['type']) ? $_GET['type'] : '0';
        $now_time = time();
        $wsql = '';
        $wsql = ' where shop_id = ' . $this->spid . ' ' . $wsql . ' ';
        $result = $this->db->get_all("select * from " . table('promote_group') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            foreach ($result as $k => $v) {
                $goods_id[] = $v['gid'];
            }
            if ($goods_id) {
                $goods_in = implode(',', $goods_id);
                $goods_result = $this->db->get_all("select id,shop_id,title,images,price,status,stock from " . table('goods') . " where id in (" . $goods_in . ") and status = 1");
                foreach ($goods_result as $k => $v) {
                    $goods_arr[$v['id']] = $v;
                }
            }
            foreach ($result as $v) {
                $v['start_time'] = _date($v['start_time']);
                $v['end_time'] = _date($v['end_time']);
                $v['addtime'] = _date($v['addtime']);
                $v['goods'] = $goods_arr[$v['gid']];
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('promote_group') . $wsql);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function group_edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('promote_group') . " where shop_id = " . $this->spid . " and id = " . $id);
            if ($row['id']) {
                $row['start_time'] = _date($row['start_time']);
                $row['end_time'] = _date($row['end_time']);
                $this->assign('row', $row);
                $this->assign('goods', $this->promote_goods($row['gid'], '2'));
            }
        } else {
            $this->assign('goods', $this->promote_goods(null, '2'));
        }
        $this->display();
    }
    function group_save()
    {
        $this->db->autocommit();
        $gid = P('gid', '请选择一个产品');
        $data['gid'] = $gid;
        $data['name'] = P('name', '名称不能为空');
        $data['num'] = P('num');
        $price_arr = isset($_POST['group_price']) ? $_POST['group_price'] : error_json('价格提交错误');
        $data['price'] = current($price_arr);
        $data['start_time'] = !empty($_POST['start_time']) ? strtotime($_POST['start_time']) : error_json('开始时间不能为空');
        $data['end_time'] = !empty($_POST['end_time']) ? strtotime($_POST['end_time']) : error_json('结束时间不能为空');
        $data['status'] = P('status', '', '0');
        $id = P('id', 0);
        if ($id) {
            $query = $this->db->update(table('promote_group'), $data, 'shop_id = ' . $this->spid . ' and id = ' . $id);
        } else {
            $row = $this->db->get_one("select * from " . table('promote_group') . " where shop_id = " . $this->spid . " and gid = " . $gid);
            if ($row['id']) {
                error_json('该产品已经存在优惠中');
            } else {
                $data['shop_id'] = $this->spid;
                $data['addtime'] = time();
                $query = $this->db->add(table('promote_group'), $data);
                $id = $this->db->insert_id();
            }
        }
        if ($price_arr) {
            $query2 = $this->db->delete(table('promote_group_sku'), 'gid = ' . $gid);
            $add_sql = '';
            foreach ($price_arr as $k => $v) {
                $add_sql .= "('" . $this->spid . "', '" . $id . "', '" . $gid . "', '" . $k . "', '" . $v . "'),";
            }
            $insert_sql = "INSERT INTO `" . table("promote_group_sku") . "` (`shop_id`, `group_id`, `gid`, `sku_id`, `price`) VALUES ";
            $insert_sql .= rtrim($add_sql, ',') . ";";
            $query3 = $this->db->query($insert_sql);
        } else {
            $this->db->rollback();
            error_json('保存失败');
        }
        if ($query && $query2 && $query3) {
            $this->db->commit();
            $result['error'] = '0';
            echo json_encode($result);
        } else {
            $this->db->rollback();
            error_json('保存失败');
        }
    }
    function group_del()
    {
    }
}